<?php
include_once(dirname(__FILE__).'/../bootstrap.php');
include_once('Persevere/Client.php');
// get a client
$p = new Persevere_Client( $config['persevere']['serverUrl'] );
// if you have authentication "on" - please 
if ($argv[1] && $argv[2]) {
  if ( $p->authenticate($argv[1], $argv[2]) ) {
    echo "Authenticated {$argv[1]} successfully ... \n";
    // @todo - currently only http basic auth works ... :/ 
    $p->setHttpBasicAuth( $argv[1], $argv[2] );  
  } else {
    echo "Authentication failed for user {$argv[1]} with given password\n";
    exit();
  }
} else {
  echo "Running script without authentication\n";
}

// add some data ...
$o1 = $p->create('Toot', array('name'=>'Toot - '.time() ) );
echo ($o1)."\n";

// simple query - get all Data -> array of Persevere_Proxyobject
$r = $p->load('Toot');
echo "Total no. toots:".sizeof($r)."\n";

// query returns if applicable -> array of Persevere_Proxyobject
$r = $p->load('Toot/[?id<5]');
// this is not an example of COUNT(*)
echo "Total no. toots w/ id less than 5:".sizeof($r)."\n";


$r = $p->load('Toot/[?id>5]');
// this is not an example of COUNT(*)
echo "Total no. toots w/ id more than 5:".sizeof($r)."\n";


// Persevere_Client introduces loadClass because persevere doesn't always populate the id field with "{Class}/{id}" but only "{id}"
// loadClass provides a "class hint" in order to allow for easy object oriented "updates" etc.
$r = $p->loadClass('Toot', '[?id<5]');
// load all data objects, order
$r = $p->loadClass('Toot', '[\id]');
// and display for good measure
echo "These should be all Toots in descending ID order\n";
foreach ($r as $r_item) {
  echo($r_item)."\n";
}

echo "Before altering object with highest id:".($r[0])."\n";
$r[0]->name = 'TootMeToot'.time();
$r[0]->update();
echo "After altering object with highest id:".($r[0])."\n";


// CRUD
$o2 = $p->create('Toot', array('name'=>'TootToot- '.time()) );
echo "Created:($o2)\n";
$o2->name=time()+999;
$o2->update();
echo "Updated:($o2)\n";
$o2->remove();
echo "Remove ".$o2->getFullId()." ...\n";
try {
  $x = $p->load( $o2->getFullId() );
  echo "Ooops ... this action should have thrown an exception \n";
} catch (Persevere_Exception $e) {
  echo "Removed ".$o2->getFullId()." successfully from persevere - could not be loaded\n";
} catch (Exception $e) {
  echo "Ooops ... this action should have thrown an exception \n";
}

// json rpc method call
// for this to work you need to have implemented these steps
// http://docs.persvr.org/documentation/how-to-create-a-class
// http://docs.persvr.org/documentation/how-to-add-a-method-to-a-class
// http://docs.persvr.org/documentation/how-to-create-a-static-method

$toot = $p->create('Toot', array('name'=>'Sample') );
echo "JSONRPC on Object({$toot->id}).setName('hello') returns ".$toot->jsonRpc('setName', array('hello'))."\n";
// @todo maybe $toot->setName('hello'); would be nicer - but that could also be a pain to debug - mixing remote and "local" calls ...

// verify that the rpc has altered the values
$tootCheck = $p->loadClass('Toot', $toot->getId() );
if ( $tootCheck->name == 'hello') {
  echo "Verified values - object(".$tootCheck->getFullId().") - name changed \n";
} else {
  echo "Verifiying values failed - object(".$tootCheck->getFullId().") - {$tootCheck->name} !== 'hello' \n";
}
$toot->remove();
echo "Static rpc class: Toot.AddThese(1.5, 2.9) = ".$p->jsonRpc('/Class/Toot', 'AddThese', array(1.5, 2.9))."\n";

